﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.ui {
    import flash.events.*;
    import reprise.css.*;
    import reprise.events.*;
    import flash.display.*;
    import reprise.core.*;
    import reprise.utils.*;
    import flash.utils.*;
    import reprise.debug.*;

    public class DocumentView extends UIComponent {

        protected var m_validatedElementsCount:int;
        protected var m_widthIsRelative:Boolean;
        protected var m_heightIsRelative:Boolean;
        protected var m_tooltipManager:TooltipManager;
        protected var m_styleSheet:CSS;
        protected var m_invalidChildren:Array;
        public var stageDimensionsChanged:Boolean;
        protected var m_focusManager:FocusManager;
        protected var m_tooltipContainer:Sprite;
        protected var m_debugInterface:DebugInterface;
        protected var m_appContext:ApplicationContext;
        protected var m_rendererFactory:UIRendererFactory;
        protected var m_parentDocument:DocumentView;
        protected var m_documentIsValidating:Boolean;
        protected var m_currentFrameTime:int;
        protected var m_elementsById:Object;

        protected static var g_totalValidationTime:int = 0;
        public static var className:String = "body";
        protected static var g_defaultStyleSheet:CSS = new CSS();

        public function DocumentView(){
            m_rendererFactory = new UIRendererFactory();
            m_elementsById = {};
        }
        override public function remove(... _args):void{
            if (parentDocument){
                parentDocument.removeChild(this);
            } else {
                if (((m_parentElement) && (!((m_parentElement == this))))){
                    m_parentElement.removeChild(this);
                };
            };
            if (parent){
                parent.removeChild(this);
            };
        }
        override protected function validateElement(_arg1:Boolean=false, _arg2:Boolean=false):void{
            super.validateElement(_arg1, _arg2);
            stageDimensionsChanged = false;
        }
        public function setFocusedElement(_arg1:UIObject, _arg2:String):Boolean{
            return (m_focusManager.setFocusedElement(_arg1, _arg2));
        }
        override protected function addComponentToDisplayList(_arg1:UIComponent, _arg2:Boolean):void{
            if (_arg1 == this){
                return;
            };
            super.addComponentToDisplayList(_arg1, _arg2);
        }
        override protected function initialize():void{
            stage.scaleMode = StageScaleMode.NO_SCALE;
            m_rootElement = this;
            m_containingBlock = this;
            m_invalidChildren = [];
            stage.addEventListener(Event.RESIZE, stage_resize, false, 0, true);
            addEventListener(Event.REMOVED_FROM_STAGE, self_removedFromStage, false, 0, true);
            super.initialize();
            stage.stageFocusRect = false;
            m_focusManager = new FocusManager(this);
            if (!parentDocument){
                m_tooltipManager = new TooltipManager(m_rootElement, m_tooltipContainer);
            };
            m_debugInterface = new DebugInterface(this);
        }
        public function uiRendererFactory():UIRendererFactory{
            return (m_rendererFactory);
        }
        protected function stage_resize(_arg1:Event):void{
            if (((stage) && (((((m_widthIsRelative) && (!((m_contentBoxWidth == stage.stageWidth))))) || (((m_heightIsRelative) && (!((m_contentBoxHeight == stage.stageHeight))))))))){
                stageDimensionsChanged = true;
                invalidateStyles();
            };
        }
        protected function self_removedFromStage(_arg1:Event):void{
            m_parentDocument = null;
            removeEventListener(Event.ENTER_FRAME, self_enterFrame);
            stage.removeEventListener(Event.RESIZE, stage_resize);
        }
        function markChildAsValid(_arg1:UIObject):void{
            var _local2:String = _arg1.toString();
            var _local3:int = m_invalidChildren.length;
            while (_local3--) {
                if (m_invalidChildren[_local3].path.indexOf(_local2) == 0){
                    m_invalidChildren.splice(_local3, 1);
                };
            };
        }
        override protected function createDisplayClips():void{
            super.createDisplayClips();
            if (parentDocument){
                return;
            };
            m_tooltipContainer = new Sprite();
            addChild(m_tooltipContainer);
        }
        override protected function refreshSelectorPath():void{
            var _local1:String = m_selectorPath;
            m_selectorPath = "";
            super.refreshSelectorPath();
            if (m_selectorPath == _local1){
                m_selectorPathChanged = false;
            };
        }
        override protected function applyStyles():void{
            super.applyStyles();
            if (m_currentStyles.frameRate){
                stage.frameRate = m_currentStyles.frameRate;
            };
            m_heightIsRelative = m_complexStyles.getStyle("height").isRelativeValue();
            m_widthIsRelative = m_complexStyles.getStyle("width").isRelativeValue();
        }
        override public function setParent(_arg1:UIObject):UIObject{
            var _local2:UIObject;
            m_rootElement = this;
            if (_arg1 == this){
                m_parentDocument = null;
            } else {
                _local2 = DisplayListUtil.locateElementContainingDisplayObject(_arg1);
                if (_local2){
                    m_parentDocument = _local2.document;
                };
            };
            super.setParent(_arg1);
            m_containingBlock = this;
            return (this);
        }
        protected function validateElements():void{
            var _local1:String;
            var _local4:String;
            var _local5:UIObject;
            if (m_invalidChildren.length == 0){
                return;
            };
            m_documentIsValidating = true;
            m_currentFrameTime = getTimer();
            m_validatedElementsCount = 0;
            var _local2:Array = m_invalidChildren.sortOn("path", Array.DESCENDING);
            m_invalidChildren = [];
            var _local3:int = _local2.length;
            while (_local3--) {
                _local4 = _local2[_local3].path;
                if (_local4.indexOf(_local1) == 0){
                } else {
                    _local1 = (_local4 + ".");
                    _local5 = UIObject(_local2[_local3].element);
                    _local5.validation_execute();
                };
            };
            g_totalValidationTime = (g_totalValidationTime + (getTimer() - m_currentFrameTime));
            log((((((("d validated " + m_validatedElementsCount) + " elements in ") + (getTimer() - m_currentFrameTime)) + "ms. ") + "Total validation time: ") + g_totalValidationTime));
            dispatchEvent(new DisplayEvent(DisplayEvent.DOCUMENT_VALIDATION_COMPLETE));
            if (m_invalidChildren.length){
                addEventListener(Event.ENTER_FRAME, self_enterFrame);
            };
            m_documentIsValidating = false;
        }
        public function get styleSheet():CSS{
            if (m_styleSheet){
                return (m_styleSheet);
            };
            return (g_defaultStyleSheet);
        }
        function registerElementID(_arg1:String, _arg2:UIComponent):void{
            m_elementsById[_arg1] = _arg2;
        }
        public function getElementById(_arg1:String):UIComponent{
            return (UIComponent(m_elementsById[_arg1]));
        }
        public function applicationContext():ApplicationContext{
            return (m_appContext);
        }
        public function setApplicationContext(_arg1:ApplicationContext):void{
            m_appContext = _arg1;
        }
        protected function self_enterFrame(_arg1:Event):void{
            removeEventListener(Event.ENTER_FRAME, self_enterFrame);
            validateElements();
        }
        function increaseValidatedElementsCount():void{
            m_validatedElementsCount++;
        }
        override protected function initDefaultStyles():void{
            m_elementDefaultStyles.setStyle("width", "100%");
            m_elementDefaultStyles.setStyle("height", "100%");
            m_elementDefaultStyles.setStyle("padding", "0");
            m_elementDefaultStyles.setStyle("margin", "0");
            m_elementDefaultStyles.setStyle("position", "absolute");
            m_elementDefaultStyles.setStyle("fontFamily", "_sans");
            m_elementDefaultStyles.setStyle("fontSize", "12px");
            m_elementDefaultStyles.setStyle("frameRate", stage.frameRate.toString());
        }
        function markChildAsInvalid(_arg1:UIObject):void{
            if ((((m_invalidChildren.length == 0)) && (!((stage == null))))){
                addEventListener(Event.ENTER_FRAME, self_enterFrame, false, 0, true);
            };
            m_invalidChildren.push({element:_arg1, path:_arg1.toString()});
        }
        public function documentIsValidating():Boolean{
            return (m_documentIsValidating);
        }
        public function set styleSheet(_arg1:CSS):void{
            m_styleSheet = _arg1;
            invalidateStyles();
            if (m_styleSheet){
                m_debugInterface.startWatchingStylesheets();
            };
        }
        public function initFromXML(_arg1:XML, _arg2:String=""):DocumentView{
            parseXMLDefinition(_arg1, _arg2);
            return (this);
        }
        public function frameTime():int{
            return (m_currentFrameTime);
        }
        override protected function applyOutOfFlowChildPositions():void{
            super.applyOutOfFlowChildPositions();
            y = m_currentStyles.marginTop;
            x = m_currentStyles.marginLeft;
        }
        public function get parentDocument():DocumentView{
            return (m_parentDocument);
        }
        override protected function resolveRelativeStyles(_arg1:CSSDeclaration, _arg2:int=-1, _arg3:int=-1):void{
            super.resolveRelativeStyles(_arg1, stage.stageWidth, stage.stageHeight);
        }
        override public function toString():String{
            return (name);
        }
        public function setUIRendererFactory(_arg1:UIRendererFactory):UIComponent{
            m_rendererFactory = _arg1;
            return (this);
        }
        function removeElementID(_arg1:String):void{
            ((m_elementsById[_arg1]) && (delete m_elementsById[_arg1]));
        }
        override public function hasHiddenAncestors():Boolean{
            return (false);
        }
        override public function get stage():Stage{
            if (realStage()){
                return (realStage());
            };
            return ((m_parentDocument) ? m_parentDocument.stage : null);
        }

    }
}//package reprise.ui 
